package com.uwo.commons.kits;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * 加密工具
 * @author hao.yan
 */
public class EncryptKits {

    /**
     * 生成MD5
     * @param message
     * @return
     */
    public static String MD5(String message) {
        try {
           return encryptMD5(message.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptMD5(byte[] data) throws Exception {
        byte[] bytes = encrypt(data, EncryptKeyEnum.MD5);
        return new HexBinaryAdapter().marshal(bytes).toLowerCase();
    }

    public static String SHA(String message) {
        try {
            return encryptSHA(message.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptSHA(byte[] bytes) throws Exception {
        return new BigInteger(1, encrypt(bytes, EncryptKeyEnum.SHA)).toString(32);
    }

    public static String SHA1(String message) {
        try {
            return encryptSHA1(message.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptSHA1(byte[] data) throws Exception {
        byte[] bytes = encrypt(data, EncryptKeyEnum.SHA1);
        return new HexBinaryAdapter().marshal(bytes).toLowerCase();
    }

    public static String SHA256(String message) {
        try {
            return encryptSHA256(message.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptSHA256(byte[] data) throws Exception {
        byte[] bytes = encrypt(data, EncryptKeyEnum.SHA256);
        return new HexBinaryAdapter().marshal(bytes).toLowerCase();
    }

    public static String SHA384(String message) {
        try {
            return encryptSHA384(message.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptSHA384(byte[] data) throws Exception {
        byte[] bytes = encrypt(data, EncryptKeyEnum.SHA384);
        return new HexBinaryAdapter().marshal(bytes).toLowerCase();
    }

    public static String SHA512(String message) {
        try {
            return encryptSHA512(message.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptSHA512(byte[] data) throws Exception {
        byte[] bytes = encrypt(data, EncryptKeyEnum.SHA512);
        return new HexBinaryAdapter().marshal(bytes).toLowerCase();
    }

    public static byte[] encrypt(byte[] data, EncryptKeyEnum encryptKeyEnum){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(encryptKeyEnum.getName());
            messageDigest.update(data);
            return messageDigest.digest();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public enum EncryptKeyEnum{

        MD5("MD5"),

        SHA("SHA"),

        SHA1("SHA1"),

        SHA224("SHA-224"),

        SHA256("SHA-256"),

        SHA384("SHA-384"),

        SHA512("SHA-512");

        private String name;

        private EncryptKeyEnum(String name){
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

}
